<template>
  <el-form label-width="auto">
    <el-form-item label="课程名称">
      <el-select-v2
        :model-value="modelValue"
        :options="options"
        :props="selectOptions"
        placeholder="请选择"
        style="width: 240px"
        @change="handleChange"
      />
    </el-form-item>
  </el-form>
  <slot>
    <el-descriptions
      v-if="topicData.id"
      direction="vertical"
      :column="4"
      border
    >
      <el-descriptions-item label="课程名称">
        {{ topicData.name }}
      </el-descriptions-item>
      <el-descriptions-item label="课题类型">
        {{ dictStore.getDictLabelForValue('topic_type', topicData.type) }}
      </el-descriptions-item>
      <el-descriptions-item label="专业领域">
        {{ topicData.subjectAreaText }}
      </el-descriptions-item>
      <el-descriptions-item label="研究开始时间">
        {{ updateTime(topicData.researchStartDate) }}
      </el-descriptions-item>
      <el-descriptions-item label="研究结束时间">
        {{ updateTime(topicData.researchEndDate) }}
      </el-descriptions-item>
      <el-descriptions-item label="预计投入经费（万元）">
        {{ topicData.estimatedBudget }}
      </el-descriptions-item>
      <el-descriptions-item label="是否需要外协">
        {{
          dictStore.getDictLabelForValue(
            'yes_no',
            topicData.externalCooperationRequired
          )
        }}
      </el-descriptions-item>
      <el-descriptions-item label="承担单位">
        {{ topicData.leadingUnitName }}
      </el-descriptions-item>
      <el-descriptions-item label="协作单位">
        {{ topicData.collaboratingUnitNames }}
      </el-descriptions-item>
      <el-descriptions-item label="课题负责人">
        {{ topicData.leader }}
      </el-descriptions-item>
      <template v-if="hasProject">
        <el-descriptions-item label="项目编号">
          {{ projectData.projectCode }}
        </el-descriptions-item>
        <el-descriptions-item label="项目名称">
          {{ projectData.projectName }}
        </el-descriptions-item>
        <el-descriptions-item label="项目等级">
          {{
            dictStore.getDictLabelForValue(
              'project_level',
              projectData.projectLevel
            )
          }}
        </el-descriptions-item>
        <el-descriptions-item label="项目类型">
          {{ projectData.projectTypeName }}
        </el-descriptions-item>
        <el-descriptions-item label="是否重点项目">
          {{ dictStore.getDictLabelForValue('yes_no', projectData.keyProject) }}
        </el-descriptions-item>
        <el-descriptions-item label="项目负责人">
          {{ projectData.headerName }}
        </el-descriptions-item>
      </template>
    </el-descriptions>
  </slot>
</template>

<script setup>
  import { selectTopicList, queryTopicAndProject } from '@/api/project/index'
  import { useDictStore } from '@/store/modules/dict'
  import { useTimeFormat } from '@/utils/useTimeFormat'

  const props = defineProps({
    modelValue: {
      type: [Number, String],
      default: '',
    },
    type: {
      type: Number,
      default: 1,
    },
    hasProject: {
      type: Boolean,
      default: false,
    },
  })
  const emit = defineEmits(['update:modelValue', 'change'])

  const { updateTime } = useTimeFormat()

  const dictStore = useDictStore()

  const options = ref([])
  const selectOptions = {
    label: 'topicName',
    value: 'topicId',
  }
  const topicData = ref({})
  const projectData = ref({})

  watch(
    () => props.modelValue,
    (id) => {
      if (id) {
        queryTopicAndProject({ topicId: id }).then((res) => {
          if (res.code == 0 || res.code == 200) {
            topicData.value = res.data.topicRespVO || {}
            projectData.value = res.data.projectInfoRespVO || {}
            emit('change', topicData.value)
          }
        })
      } else {
        topicData.value = []
        projectData.value = []
      }
    }
  )

  const getTopic = () => {
    selectTopicList({ type: props.type }).then((res) => {
      if (res.code == 0 || res.code == 200) {
        options.value = res.data || []
      }
    })
  }

  const handleChange = (value) => {
    emit('update:modelValue', value)
  }

  getTopic()
</script>

<style lang="scss" scoped></style>
